package com.service;

import com.dao.RoleMapper;
import com.dao.RoleMenuMapper;
import com.pojo.Role;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    public void insert(Role role) {
        roleMapper.insert(role);
    }

    public void update(Role role) {
        roleMapper.update(role);
    }

    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    public List<Integer> getAssignedMenuIds(Integer roleId) {
        return roleMenuMapper.getMenuIdsByRoleId(roleId);
    }

    public void saveAssignedMenus(Integer roleId, List<Integer> menuIds) {
        roleMenuMapper.deleteByRoleId(roleId);
        for (Integer menuId : menuIds) {
            roleMenuMapper.insert(roleId, menuId);
        }
    }
}
